package com.spx.core.ui.properties;

import org.apache.commons.lang.StringUtils;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.jface.preference.PreferencePage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.dialogs.PropertyPage;

import com.spx.core.CoreActivator;
import com.spx.core.types.User;

public class BasicUserPropertyPage extends PropertyPage
{

	private static final String NAME_TITLE = "Name:";
	private static final String EMAIL_TITLE = "Email:";
	private static final String ENABLED_TITLE = "Enabled:";
	private static final String LASTLOGIN_TITLE = "Last Login:";
	private static final String PASSWORD_TITLE = "Password:";
	private static final String PASSWORD_TITLE2 = "Repeat Password:";

	private static final int TEXT_FIELD_WIDTH = 50;

	private Text nameText;
	private User user = new User();
	private String userID;
	private Text userNameText;
	private Text userEmailText;
	private Text userPasswordText;
	private Text userPassword2Text;
	private Button userEnabledCheckbox;

	/**
	 * Constructor for SamplePropertyPage.
	 */
	public BasicUserPropertyPage()
	{
		super();
	}

	private void addFirstSection(Composite composite)
	{

		final IAdaptable userAdaptor = getElement();
		user = (User) userAdaptor.getAdapter(User.class);
		userID = user.getId();
		// Label for path field
		final Label nameLabel = new Label(composite, SWT.NONE);
		nameLabel.setText(NAME_TITLE);
		userNameText = new Text(composite, SWT.WRAP | SWT.BORDER);

		userNameText.setText(user.getName() + "");
		final GridData gridData = new GridData();
		gridData.horizontalAlignment = GridData.FILL;
		gridData.grabExcessHorizontalSpace = true;
		userNameText.setLayoutData(gridData);

		// Label for path field
		final Label emailLabel = new Label(composite, SWT.WRAP | SWT.BORDER);
		emailLabel.setText(EMAIL_TITLE);
		userEmailText = new Text(composite, SWT.WRAP | SWT.BORDER);
		userEmailText.setText(user.getEmailAddress() + "");
		userEmailText.setLayoutData(gridData);
		// Label for path field
		final Label passwordLabel = new Label(composite, SWT.WRAP | SWT.BORDER);
		passwordLabel.setText(PASSWORD_TITLE);
		userPasswordText = new Text(composite, SWT.PASSWORD | SWT.BORDER);
		userPasswordText.setText(user.getPassword() + "");
		userPasswordText.setLayoutData(gridData);
		// Label for path field
		final Label password2Label = new Label(composite, SWT.NONE);
		password2Label.setText(PASSWORD_TITLE2);
		userPassword2Text = new Text(composite, SWT.PASSWORD | SWT.BORDER);
		userPassword2Text.setText(user.getPassword() + "");
		userPassword2Text.setLayoutData(gridData);
		// Label for path field
		final Label enabledLabel = new Label(composite, SWT.NONE);
		enabledLabel.setText(ENABLED_TITLE);
		userEnabledCheckbox = new Button(composite, SWT.WRAP | SWT.CHECK);
		userEnabledCheckbox.setSelection(user.isDisabled() == false);

	}

	/**
	 * @see PreferencePage#createContents(Composite)
	 */
	@Override
	protected Control createContents(Composite parent)
	{
		final Composite composite = createDefaultComposite(parent);
		addFirstSection(composite);

		return composite;
	}

	private Composite createDefaultComposite(Composite parent)
	{

		final Composite composite = new Composite(parent, SWT.NONE);
		final GridLayout layout = new GridLayout(2, true);
		composite.setLayout(layout);
		final GridData data = new GridData(GridData.FILL);
		data.grabExcessHorizontalSpace = true;
		composite.setLayoutData(data);

		return composite;
	}

	@Override
	protected void performDefaults()
	{

	}

	@Override
	public boolean performOk()
	{

		user.setName(userNameText.getText());
		user.setEmailAddress(userEmailText.getText());
		user.setDisabled(userEnabledCheckbox.getSelection());
		if (StringUtils.equals(userPasswordText.getText(), userPassword2Text.getText()))
		{
			user.setPassword(userPasswordText.getText());
		}

		CoreActivator.getDefault().getSecurityService().modify(user);
		// store the value in the owner text field

		return true;
	}

}